package wang.unclecat.droidlib;

import android.R;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.lang.reflect.Field;
import java.util.Map;

public class CatDroid {
    // 通过Android API，向鸿蒙应用界面中添加控件
    public static void bringToFront() {
        Activity activity = getCurrentActivity();
        FrameLayout decorView = (FrameLayout) activity.getWindow().getDecorView();
        FrameLayout contentView = decorView.findViewById(R.id.content);
        FrameLayout frameLayout = (FrameLayout) contentView.getChildAt(0);//和WebView并列在contentView下面
        SurfaceView agpContainerView = (SurfaceView) frameLayout.getChildAt(0);
        agpContainerView.setZOrderOnTop(true);
        agpContainerView.getHolder().setFormat(PixelFormat.TRANSPARENT);
    }

    public static Object addView() {
        Activity activity = getCurrentActivity();
        FrameLayout decorView = (FrameLayout) activity.getWindow().getDecorView();
        FrameLayout contentView = decorView.findViewById(R.id.content);


        LinearLayout linearLayout = new LinearLayout(activity);
        int padding = 60;
        linearLayout.setPadding(padding, padding, padding, padding);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        ColorDrawable colorDrawable = new ColorDrawable();
        colorDrawable.setColor(Color.GRAY);
        colorDrawable.setAlpha(210);
        linearLayout.setBackground(colorDrawable);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER;
        linearLayout.setLayoutParams(params);


        LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        params1.bottomMargin = 10;
        ProgressBar progressBar = new ProgressBar(activity);
        progressBar.setIndeterminate(true);
        progressBar.setLayoutParams(params1);

        TextView textView = new TextView(activity);
        textView.setText("加载中...");
        textView.setTextSize(20);
        textView.setLayoutParams(params1);

        linearLayout.addView(progressBar);
        linearLayout.addView(textView);
        contentView.addView(linearLayout);

        return linearLayout;
    }


    public static void removeView(Object droidView) {
        View view = (View) droidView;
        Activity activity = getCurrentActivity();
        FrameLayout decorView = (FrameLayout) activity.getWindow().getDecorView();
        FrameLayout contentView = decorView.findViewById(R.id.content);
        contentView.removeView(view);
    }

    public static void setVisibility(Object droidView, boolean visible) {
        View view = (View) droidView;
        if (visible) {
            view.setVisibility(View.VISIBLE);
        } else {
            view.setVisibility(View.GONE);
        }
    }

    //反射获取Android的Activity
    public static Activity getCurrentActivity () {
        try {
            Class activityThreadClass = Class.forName("android.app.ActivityThread");
            Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
            Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
            activitiesField.setAccessible(true);
            Map activities = (Map) activitiesField.get(activityThread);
            for (Object activityRecord : activities.values()) {
                Class activityRecordClass = activityRecord.getClass();
                Field pausedField = activityRecordClass.getDeclaredField("paused");
                pausedField.setAccessible(true);
                if (!pausedField.getBoolean(activityRecord)) {
                    Field activityField = activityRecordClass.getDeclaredField("activity");
                    activityField.setAccessible(true);
                    Activity activity = (Activity) activityField.get(activityRecord);
                    return activity;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
